import router from './router'
import store from './store'
import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css' // Progress 进度条样式
import { secondDirectory, tabList } from '@/utils/config.js'
import { getToken } from '@/utils/auth' // 验权
import {
    Message
} from 'element-ui'
import Layout from './views/layout/Layout'

const whiteList = ['/login'] // 不重定向白名单
router.beforeEach((to, from, next) => {
    NProgress.start()
    // 刷新判断当前是否登录状态
    let loginName = window.localStorage.getItem('admin') ? JSON.parse(window.localStorage.getItem('admin')) : ''
    if (getToken('token') != 'undefined' && getToken()) {
				let menuList = JSON.parse(window.localStorage.getItem("menuList"));
				for (let i in secondDirectory) {
					menuList.forEach((item) => {
						if (item.name == i) {
							item.childMenu.forEach((cel, index) => 　{
								secondDirectory[i].list.forEach((child, childIndex) => {
									if(cel.menuName == child.val) {
										secondDirectory[i].list[childIndex].show = cel.select
									}
								})
								tabList[i][0].title = item.childMenu[0].menuName
							})
						}
					})
				}
        // 如果当前是登录路由 跳转首页
       to.path === '/login' ? next({
       	path: '/'
       }) : next()
        NProgress.done()
        store.dispatch('SET_TAB_CURRENT_INDEX', 1)
        // 记录当前路由对象的 二级分类 和tabs显示
        store.dispatch('SET_SECOND_DIRECTORY', secondDirectory[to.name])
        store.dispatch('SET_TAB_LIST', tabList[to.name])
    } else {
        if (whiteList.indexOf(to.path) !== -1) {
            next()
        } else {
            next('/login')
            NProgress.done()
        }
    }
})

router.afterEach(() => {
    NProgress.done() // 结束Progress
})
